package model

import (
	"github.com/jinzhu/gorm"
	"time"
	"github.com/satori/go.uuid"
)

type AuthorizationCode struct {
	gorm.Model
	ClientID    uint `sql:"index;not null"`
	UserID      uint `sql:"index;not null"`
	Client      *Client
	Account     *Account
	Code        string         `sql:"type:varchar(40);unique;not null"`
	RedirectURI string `sql:"type:varchar(200)"`
	ExpiresAt   time.Time      `sql:"not null;default:now()"`
	Scope       string         `sql:"type:varchar(200);not null"`
}


// NewAuthorizationCode creates new AuthorizationCode instance
func NewAuthorizationCode(client *Client, user *Account, expiresIn int, redirectURI, scope string) *AuthorizationCode {
	v1,_:=uuid.NewV1()
	return &AuthorizationCode{
		ClientID:    client.ID,
		UserID:      user.ID,
		Code:        v1.String(),
		ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		RedirectURI: redirectURI,
		Scope:       scope,
	}
}